<?php
/**
 * @version 1.6
 * @package RealEstateManager New free
 * @copyright 2009 OrdaSoft
 * @author 2009 Sergey Drughinin-OrdaSoft(Sergey.dru@gmail.com)
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @description RealEstateManager New for RealEstateManager Component
*/


/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

global $mosConfig_live_site, $mosConfig_absolute_path;
$doc =& JFactory::getDocument(); 
$doc->addStyleSheet($mosConfig_live_site.'/administrator/components/com_realestatemanager/includes/realestatemanager.css');

$database = &JFactory::getDBO();

if ( !function_exists('getWhereUsergroupsString')) {
  function getWhereUsergroupsString( $table_alias ) { 
    global $my;
     
    if ( isset($my->id) AND $my->id != 0 ) { 
     
      $usergroups_sh = getGroupsByUser ($my->id,'');       
  
      //$usergroups_sh = '-2'.$usergroups_sh;   
      
    } else {
       $usergroups_sh = array ();
    }
    $usergroups_sh[] = -2; 
      
          
    $s = '';
    for ($i=0; $i<count($usergroups_sh); $i++) {  
      $g = $usergroups_sh[$i];
      $s .= " $table_alias.params LIKE '%,{$g}' or $table_alias.params = '{$g}' or $table_alias.params LIKE '{$g},%' or $table_alias.params LIKE '%,{$g},%' ";
      if ( ($i+1)<count($usergroups_sh) )
        $s .= ' or ';
    } 
    return $s;
  } 
}

$s=GetWhereUserGroupsString("c");

if( !function_exists( 'sefreltoabs')) {
  function sefRelToAbs( $value ) {
    //Need check!!!

    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);
    if(substr(strtolower($url),0,9) != "index.php") return $url;
    $uri    = JURI::getInstance();
    $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}

$selectstring = "SELECT id  FROM #__menu WHERE menutype like '%menu%' AND link LIKE '%option=com_realestatemanager%'";
$database->setQuery($selectstring);
$ItemId_tmp_from_db = $database->loadResult();


$moduleclass_sfx = $params->get('moduleclass_sfx', '');
$count = intval($params->get('count',1));
$g_words = $params->get('words','');
$showtitle = $params->get ('showtitle', '');
$showauthor = $params->get ('showbroker', '');
$showcover	= $params->get ('showimage', 1);
$displaytype	= $params->get ('displaytype', 0);
$coversize	= $params->get ('imagesize', '127');
$sortnewby	= $params->get ('sortnewby', 0);
$ItemId_tmp_from_params=$params->get('ItemId');
if($ItemId_tmp_from_params==""){
  $ItemId_tmp=$ItemId_tmp_from_db;
}
else{
  $ItemId_tmp=$ItemId_tmp_from_params;
}

switch($sortnewby) {
case 0:
	$sql_orderby_query = "date";	// Last Edited
	break;
case 1:
	$sql_orderby_query	= "id";		// Last Added
	break;
}

$selectstring = "SELECT h.id, h.houseid, h.htitle, h.description, h.broker, h.image_link, hc.idcat
		            \nFROM #__rem_houses AS h
                \nLEFT JOIN #__rem_categories AS hc ON hc.iditem=h.id
		            \nLEFT JOIN #__rem_main_categories AS c ON c.id=hc.idcat
		            \nWHERE ($s) AND h.published=1
           	    \nORDER BY ".$sql_orderby_query." DESC LIMIT 0,$count;"; 

$database->setQuery($selectstring);
$rows = $database->loadObjectList();

?> 
<div class="realestatemanager_<?php echo $moduleclass_sfx?>" >
    <table cellpadding="1" cellspacing="1" class="basictable" width="100%">
<?php	
 if($displaytype==1) { // Display Horizontal         
?>
     <tr valign="top">           
<?php	
} 
?>
        
<?php foreach ($rows as $row) { 
		$comment = $row->description;
		$prevwords = count(explode(" ",$comment));
		if(trim($g_words == "" )) $words = $prevwords;
 		else $words = intval($g_words);
		$text = implode(" ", array_slice(explode(" ",$comment), 0, $words));
		if (count(explode(" ",$text))<$prevwords){
		  $text .= "";
		}
	
		   $link1 ="index.php?option=com_realestatemanager&amp;task=view&amp;id=".$row->id."&amp;catid=".$row->idcat."&amp;Itemid=".$ItemId_tmp;
		//for local images
		$imageURL = $row->image_link;
    
    
      $mosConfig_absolute_path=JPATH_BASE;
      $mosConfig_live_site=JURI::base(true);
    if($imageURL!=''){  
        $file_pth= pathinfo($imageURL);
        $file_type=".".$file_pth['extension'];
        if(array_key_exists  ( 'filename' , $file_pth  ) ) $file_name=$file_pth['filename'];
        else $file_name = substr($imageURL, 0,strlen($imageURL)-strlen($file_pth['extension'] ) -1 );
        $file=$mosConfig_absolute_path . '/components/com_realestatemanager/photos/'. $file_name ."_gallery". $file_type;
        if(!file_exists($file)){
          $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name."_mini".$file_type;
        }
        else $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name."_gallery".$file_type;
        $imageURL=$file;
    }
		else $imageURL = "./components/com_realestatemanager/images/no-img_eng.gif";

if($displaytype==1) { // Display Horizontal         
  if($showcover==1) { 
  ?>
        <td>
            <a href="<?php echo sefRelToAbs( $link1 ); ?>" target="_self">
 <img src="<?php echo $imageURL; ?>"  hspace="15" vspace="2" border="0" height="<?php echo $coversize; ?>" /></a>
		</td>
 <?php 
 	} //End Show Image If 
 ?>
		<td valign="top">
			<p><strong><?php 
			   if ($showtitle == "1") {
			   echo $row->htitle; 
			   } else {
			   print "&nbsp;";
			   }   
			?></strong><br/>By&nbsp;<?php
			   if ($showauthor == "1") {
			   echo $row->broker;
			   } else {
			   print "&nbsp;";
			   }
			?><p><?php echo $text; ?></p>
		   <p><a class="readon" href="<?php echo sefRelToAbs( $link1 ); ?>" target="_self">Read Reviews...</a></p>
	      		
<?php 
	} else { 
//Display Vertical     
 ?>
 		<tr valign="top">
          <td>
            <a href="<?php echo sefRelToAbs( $link1 ); ?>" target="_self">
            <?php if($showcover==1) { ?>             
	            <img src="<?php echo $imageURL; ?>"  hspace="2" vspace="2" border="0" height="<?php echo $coversize; ?>" /></a>
			<?php } //End Show Image If?>
            <?php 
            if ($showtitle == "1") {
				echo $row->htitle; 
            } else {
        	    echo "&nbsp;";
            }   
            ?>  
                        
            <?php 
            if ($showauthor == "1") { 
				echo "<br />".$row->broker;
            } else {
	            echo "&nbsp;";
            }
            ?>
            <br />
            <p><?php echo $text; ?></p>
			<p><a class="readon" href="<?php echo sefRelToAbs( $link1 ); ?>" target="_self">Read more...</a></p>
            </td>            
        </tr>    
        <tr> <td>&nbsp; </td>
        </tr>
<?php
	} //End Display If
}
?>
<?php	
 if($displaytype==1) { // Display Horizontal         
?>
     </tr>           
<?php	
} 
?>

   </table>
<div style="text-align: center;"><a href="http://ordasoft.com" style="font-size: 10px;">Powered by OrdaSoft!</a></div>
</div>